<?php 
/**
 * 
 * @author tangbl
 *
 */
class MenuModel extends Model {

	/*array(
		0.字段名   
		1.验证规则 [require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字]
		2.提示信息
		3.验证条件 [0.(默认)存在字段就验证;1.必须验证;2.值不为空的时候验证]
		4.附加规则 [regex、function、callback、confirm、equal、in、length、between、expire、ip_allow、ip_deny、unique]
		5.验证时   [1.新增数据时候验证;2.编辑数据时候验证;3.(默认)全部情况下验证]
	);*/
	protected $_validate=array(
		array('url','require','{%cfg_lianjie_require}',1,'',3),
		//array('name','require','{%nod_form_validata_name_required}',1,'',1),
		//array('level',array(0,1,2,3),'{%nod_form_validata_level_required}',1,'in',3),
		//array('permiss',array(1,2),'{%nod_form_validata_permiss_req}',1,'in',3),
	);

	// 获取所有节点信息
	public function getAllNode($where = '' , $order = 'sort DESC') {
		return $this->where($where)->order($order)->select();
	}
	
	//根据节点生成select
	public function createNodeSelect($pid=0){
		
		$model   = D("Node");
		$allNode = $model->getAllNode();
		$array   = array();
		$lang    = getLang();
		foreach($allNode as $k => $r) {
			$r['id']         = $r['id'];
			$r['title']      = dejsonByLang($r['langval'],$lang);
			$r['name']       = $r['name'];
			$r['url']        = $r['data'];
			$r['disabled']   = ($r['level']<=1) ? 'disabled' : '';
			$r['selected']   = '';  
			if($r['id'] == $pid){
				$r['selected'] = "selected";
			}
			$array[] = $r;
		}
		return $array;
	}

	// 获取单个节点信息
	public function getNode($where = '',$field = '*') {
		return $this->field($field)->where($where)->find();
	}

	// 删除节点
	public function delNode($where) {
		if($where){
			return $this->where($where)->delete();
		}else{
			return false;
		}
	}

	// 更新节点
	public function upNode($data) {
		if($data){
			return $this->save($data);
		}else{
			return false;
		}
	}

	// 子节点
	public function childNode($id){
		return $this->where(array('pid'=>$id))->select();
	}
	
	//更新字典缓存
	public function getMenuByKey($key,$flag=true) {
	
		$list = F('Dict_'.$key);
		if($flag || empty($list)){
			$map['key'] = $key ;
			$order = "sort desc";
			$list  = $this->where($map)->order($order)->select();
			F('Dict_'.$key,$list);
		}
		return $list;
	}
	
	//更新父级字典是否有下级(haveLeaf)
	public function updLeaf($id,$flag=false){
		$haveLeaf  = 1;
		if($flag){
			$num = $this->where("pid = $id")->count();
			if($num == 0){
				$haveLeaf = 2 ;
			}
		}
	
		$data['id']        = $id;
		$data['haveLeaf']  = $haveLeaf;
		$this->data($data)->save();
	}
	
	//生成字典level、haveLeaf、
	public function genMenu($pid,$id){
	
		if($pid){
			//更新父节点
			$this->updLeaf($pid,false);
				
			//更新level
			$parent = $this->find($pid);
			$data['level'] = intval($parent['level'])+1;
			$map['id']     = $id ;
			$this->where($map)->save($data);
				
		}else{
			$data['level']    = 1 ;
			$data['haveLeaf'] = 2 ;
			$data['id']       = $id ;
			$this->data($data)->save();	
		}
		//更新对应key的缓存
		//$this->getMenuByKey();
	}
	
	
}